import { IsInt, IsString, IsEnum, IsDateString, IsNumber, IsOptional, IsObject, Min, Max } from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { TaskStatus } from './task-list.dto';

/**
 * 任务响应DTO - 基础版本
 */
export class TaskResponseDto {
  @ApiProperty({
    description: '任务ID',
    example: 123,
  })
  @IsInt({ message: '任务ID必须是整数' })
  task_id: number;

  @ApiProperty({
    description: '患者编号',
    example: 'PAT-2024-001',
  })
  @IsString({ message: '患者编号必须是字符串' })
  patient_code: string;

  @ApiProperty({
    description: '任务状态',
    enum: TaskStatus,
    example: TaskStatus.COMPLETED,
  })
  @IsEnum(TaskStatus, { message: '任务状态值无效' })
  status: TaskStatus;

  @ApiProperty({
    description: '提交时间',
    example: '2024-01-01T10:00:00Z',
    type: 'string',
    format: 'date-time',
  })
  @IsDateString({}, { message: '提交时间格式无效' })
  submit_time: string;

  @ApiPropertyOptional({
    description: '完成时间',
    example: '2024-01-01T10:05:00Z',
    type: 'string',
    format: 'date-time',
  })
  @IsDateString({}, { message: '完成时间格式无效' })
  @IsOptional()
  complete_time?: string;

  @ApiPropertyOptional({
    description: '评分结果',
    example: 3,
    minimum: 0,
    maximum: 8,
  })
  @IsInt({ message: '评分必须是整数' })
  @Min(0, { message: '评分不能小于0' })
  @Max(8, { message: '评分不能大于8' })
  @IsOptional()
  score?: number;

  @ApiPropertyOptional({
    description: '严重程度',
    example: 'Moderate',
  })
  @IsString({ message: '严重程度必须是字符串' })
  @IsOptional()
  severity?: string;

  @ApiPropertyOptional({
    description: '置信度',
    example: 0.85,
    minimum: 0,
    maximum: 1,
  })
  @IsNumber({}, { message: '置信度必须是数字' })
  @Min(0, { message: '置信度不能小于0' })
  @Max(1, { message: '置信度不能大于1' })
  @IsOptional()
  confidence?: number;
}

/**
 * 患者信息DTO
 */
export class PatientInfoDto {
  @ApiProperty({
    description: '患者ID',
    example: 456,
  })
  @IsInt({ message: '患者ID必须是整数' })
  patient_id: number;

  @ApiProperty({
    description: '患者编号',
    example: 'PAT-2024-001',
  })
  @IsString({ message: '患者编号必须是字符串' })
  patient_code: string;

  @ApiPropertyOptional({
    description: '性别',
    example: 1,
    enum: [0, 1, 2],
  })
  @IsInt({ message: '性别必须是整数' })
  @IsOptional()
  gender?: number;

  @ApiPropertyOptional({
    description: '生日',
    example: '1980-01-01',
    type: 'string',
    format: 'date',
  })
  @IsDateString({}, { message: '生日格式无效' })
  @IsOptional()
  birthday?: string;
}

/**
 * AI分析结果DTO
 */
export class AiAnalysisResultDto {
  @ApiProperty({
    description: '评分',
    example: 3,
    minimum: 0,
    maximum: 8,
  })
  @IsInt({ message: 'AI评分必须是整数' })
  @Min(0, { message: 'AI评分不能小于0' })
  @Max(8, { message: 'AI评分不能大于8' })
  score: number;

  @ApiProperty({
    description: '严重程度',
    example: 'Moderate',
  })
  @IsString({ message: 'AI严重程度必须是字符串' })
  severity: string;

  @ApiProperty({
    description: '置信度',
    example: 0.85,
    minimum: 0,
    maximum: 1,
  })
  @IsNumber({}, { message: 'AI置信度必须是数字' })
  @Min(0, { message: 'AI置信度不能小于0' })
  @Max(1, { message: 'AI置信度不能大于1' })
  confidence: number;

  @ApiProperty({
    description: '模型版本',
    example: 'v2.1.0',
  })
  @IsString({ message: '模型版本必须是字符串' })
  model_version: string;

  @ApiProperty({
    description: '分析详情',
    example: '基于图像特征分析得出...',
  })
  @IsString({ message: '分析详情必须是字符串' })
  analysis_details: string;

  @ApiProperty({
    description: '推理时间(毫秒)',
    example: 2500,
  })
  @IsInt({ message: '推理时间必须是整数' })
  inference_time: number;
}

/**
 * 任务详情响应DTO - 完整版本
 */
export class TaskDetailResponseDto extends TaskResponseDto {
  @ApiProperty({
    description: '图像URL',
    example: '/uploads/medical_images/PAT-2024-001_123456.jpg',
  })
  @IsString({ message: '图像URL必须是字符串' })
  image_url: string;

  @ApiPropertyOptional({
    description: '临床文本',
    example: '患者主诉腹痛，伴有便血症状',
  })
  @IsString({ message: '临床文本必须是字符串' })
  @IsOptional()
  clinical_text?: string;

  @ApiPropertyOptional({
    description: '患者信息',
    type: PatientInfoDto,
  })
  @IsObject({ message: '患者信息必须是对象' })
  @IsOptional()
  patient?: PatientInfoDto;

  @ApiPropertyOptional({
    description: 'AI分析结果',
    type: AiAnalysisResultDto,
  })
  @IsObject({ message: 'AI分析结果必须是对象' })
  @IsOptional()
  ai_result?: AiAnalysisResultDto;
}